home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / etc / init.d / umountfs < prev    next >
Text File  |  2008-10-14  |  4KB  |  185 lines

  1. #! /bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides:          umountfs
  4. # Required-Start:
  5. # Required-Stop:     umountroot
  6. # Default-Start:
  7. # Default-Stop:      0 6
  8. # Short-Description: Turn off swap and unmount all local file systems.
  9. # Description:
  10. ### END INIT INFO
  11.  
  12. PATH=/sbin:/usr/sbin:/bin:/usr/bin
  13. . /lib/init/vars.sh
  14.  
  15. . /lib/lsb/init-functions
  16.  
  17. umask 022
  18.  
  19. # Print in order of decreasing length
  20. #
  21. # Algorithm: Find and print longest argument, then call self
  22. # to print remaining arguments in order of decreasing length
  23. #
  24. # This function runs at one tenth the speed of the sort program
  25. # but we use the function because we don't want to rely on any
  26. # programs in /usr/.
  27. #
  28. # N.B.: Arguments must not be null and must not contain whitespace
  29. #
  30. pioodl() {
  31.     [ "$1" ] || return 0
  32.     ARGNUM=1
  33.     ARGNUM_LONGEST=0
  34.     ARGLENGTH_LONGEST=0
  35.     for ARG in "$@"
  36.     do
  37.         ARGLENGTH="${#ARG}"
  38.         if [ "$ARGLENGTH" -gt "$ARGLENGTH_LONGEST" ]
  39.         then
  40.             ARGLENGTH_LONGEST="$ARGLENGTH"
  41.             ARGNUM_LONGEST="$ARGNUM"
  42.         fi
  43.         ARGNUM=$(($ARGNUM + 1))
  44.     done
  45.     # The method of passing prevargs assumes that args can be
  46.     # delimited with spaces
  47.     ARGNUM=1
  48.     PREVARGS=""
  49.     while [ "$ARGNUM" -lt "$ARGNUM_LONGEST" ]
  50.     do
  51.         PREVARGS="$PREVARGS $1"
  52.         shift
  53.         ARGNUM=$(($ARGNUM + 1))
  54.     done
  55.     echo "$1"
  56.     shift
  57.     pioodl $PREVARGS "$@"
  58. }
  59.  
  60.  
  61. do_stop () {
  62.     exec 9<&0 </proc/mounts
  63.  
  64.     PROTECTED_MOUNTS="$(sed -n '0,/^\/[^ ]* \/ /p' /proc/mounts)"
  65.     WEAK_MTPTS="" # be gentle, don't use force
  66.     REG_MTPTS=""
  67.     TMPFS_MTPTS=""
  68.     while read DEV MTPT FSTYPE REST
  69.     do
  70.         echo "$PROTECTED_MOUNTS" | grep -qs "^$DEV $MTPT " && continue
  71.         case "$MTPT" in
  72.           /|/proc|/dev|/.dev|/dev/pts|/dev/shm|/dev/.static/dev|/proc/*|/sys|/lib/init/rw)
  73.             continue
  74.             ;;
  75.           /var/run)
  76.             if [ yes = "$RAMRUN" ] ; then
  77.                 continue
  78.             fi
  79.             ;;
  80.           /var/lock)
  81.             if [ yes = "$RAMLOCK" ] ; then
  82.                 continue
  83.             fi
  84.             ;;
  85.         esac
  86.         case "$FSTYPE" in 
  87.           proc|procfs|linprocfs|devfs|sysfs|usbfs|usbdevfs|devpts)
  88.             continue
  89.             ;;
  90.           tmpfs)
  91.             TMPFS_MTPTS="$TMPFS_MTPTS $MTPT"
  92.             ;;
  93.           *)
  94.             if echo "$PROTECTED_MOUNTS" | grep -qs "^$DEV "; then
  95.                 WEAK_MTPTS="$WEAK_MTPTS $MTPT "
  96.             else
  97.                 REG_MTPTS="$REG_MTPTS $MTPT"
  98.             fi
  99.             ;;
  100.         esac
  101.     done
  102.  
  103.     exec 0<&9 9<&-
  104.     
  105.     #
  106.     # Make sure tmpfs file systems are umounted before turning off
  107.     # swap, to avoid running out of memory if the tmpfs filesystems
  108.     # use a lot of space.
  109.     #
  110.     if [ "$TMPFS_MTPTS" ]
  111.     then
  112.         TMPFS_MTPTS="$(pioodl $TMPFS_MTPTS)"
  113.         if [ "$VERBOSE" = no ]
  114.         then
  115.             log_action_begin_msg "Unmounting temporary filesystems"
  116.             umount $TMPFS_MTPTS
  117.             log_action_end_msg $?
  118.         else
  119.             log_daemon_msg "Will now unmount temporary filesystems"
  120.             umount -v $TMPFS_MTPTS
  121.             log_end_msg $?
  122.         fi
  123.     fi
  124.  
  125.     #
  126.     # Deactivate swap
  127.     #
  128.     if [ "$VERBOSE" = no ]
  129.     then
  130.         log_action_begin_msg "Deactivating swap"
  131.         swapoff -a >/dev/null
  132.         log_action_end_msg $?
  133.     else
  134.         log_daemon_msg "Will now deactivate swap"
  135.         swapoff -a -v
  136.         log_end_msg $?
  137.     fi
  138.  
  139.     #
  140.     # Unmount local filesystems
  141.     #
  142.     if [ "$WEAK_MTPTS" ]; then
  143.         WEAK_MTPTS="$(pioodl $WEAK_MTPTS)"
  144.         if [ "$VERBOSE" = no ]
  145.         then
  146.             umount -r -d $WEAK_MTPTS
  147.         else
  148.             umount -v -r -d $WEAK_MTPTS
  149.         fi
  150.     fi
  151.     if [ "$REG_MTPTS" ]
  152.     then
  153.         REG_MTPTS="$(pioodl $REG_MTPTS)"
  154.         if [ "$VERBOSE" = no ]
  155.         then
  156.             log_action_begin_msg "Unmounting local filesystems"
  157.             umount -f -r -d $REG_MTPTS
  158.             log_action_end_msg $?
  159.         else
  160.             log_daemon_msg "Will now unmount local filesystems"
  161.             umount -f -v -r -d $REG_MTPTS
  162.             log_end_msg $?
  163.         fi
  164.     fi
  165. }
  166.  
  167. case "$1" in
  168.   start)
  169.     # No-op
  170.     ;;
  171.   restart|reload|force-reload)
  172.     echo "Error: argument '$1' not supported" >&2
  173.     exit 3
  174.     ;;
  175.   stop)
  176.     do_stop
  177.     ;;
  178.   *)
  179.     echo "Usage: $0 start|stop" >&2
  180.     exit 3
  181.     ;;
  182. esac
  183.  
  184. :
  185.